﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CCWin;
using GroupChat2013.Entity;
using GroupChat2013.Common;
using GroupChat2013.Service;
using GroupChat2013.Logic;
using System.Web;

namespace GroupChat2013.Main
{
    public partial class FrmAddGroup : CCSkinMain
    {
        private GroupInfo groupInfo;

        public event Action CompletedHandler;

        public FrmAddGroup(GroupInfo groupInfo)
        {
            InitializeComponent();
            this.Load += new EventHandler(FrmAddGroup_Load);

            this.Name = groupInfo == null || string.IsNullOrEmpty(groupInfo.groupId) ?
                "创建群" : "修改群名称";

            this.groupInfo = groupInfo;
        }

        void FrmAddGroup_Load(object sender, EventArgs e)
        {
            if (groupInfo != null)
                tbGroupName.SkinTxt.Text = groupInfo.groupName;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbGroupName.SkinTxt.Text.Trim()))
            {
                MessageBoxEx.Show("请输入群名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            try
            {
                AsyncClient.AsyncCall<GroupInfo>(() =>
                {
                    string name = HttpUtility.UrlEncode(tbGroupName.SkinTxt.Text);
                    //添加
                    if (groupInfo == null || string.IsNullOrEmpty(groupInfo.groupId))
                    {                        
                        return ServiceUrlProxy.GetInstance().AddGroup(name,
                            EngineContext.Current.CurrUserInfo.CID);
                    }
                    else
                    {
                        return ServiceUrlProxy.GetInstance().UpdateGroup(groupInfo.groupId, name,
                                       EngineContext.Current.CurrUserInfo.CID);
                    }
                },
                (group) =>
                {
                    if (group != null)
                    {
                        MessageBoxEx.Show(groupInfo == null || string.IsNullOrEmpty(groupInfo.groupId) ?
                            "创建群成功" : "修改群成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        if (CompletedHandler != null)
                            CompletedHandler();

                        this.Close();
                        return;
                    }
                });
            }
            catch (Exception ex)
            {
                LogHelper.LogError(string.Format("AddGroup()/UpdateGroup()方法 错误信息：{0} 堆栈信息：{1} ", ex.Message, ex.StackTrace));
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
